namespace("jclass.events")
({
    IEvent : jInterface({
        stopEventPropagation : function() {},
        clone : function() {}
    })
});

namespace("jclass.events")
({    
    EventDispatcher : jClass({
        public : {
            AddEventListener : function(eventType, callback) {
                if (!this.HasEventListener(eventType, callback)) {
                    if (typeof registeredEvents[eventType] === 'undefined') {
                        registeredEvents[eventType] = new Array();
                    }
                }
                 if (typeof callback === 'function') {
                    registeredEvents[eventType].push(callback);
                } else {
                    throw new TypeError("Callback must be a function");
                }
                return this.HasEventListener(eventType, callback);
            },

            RemoveEventListener : function(eventType, callback) {
                if (!this.HasEventListener(eventType, callback)) {
                    var index = registeredEvents[eventType].indexOf(callback);
                    if (index != -1) {
                        registeredEvents[eventType].splice(index, 1);
                        return (registeredEvents[eventType].indexOf(callback) == -1);
                    } else {
                        return false;
                    }
                }
                return false;
            },

            DispatchEvent : function(event) {
                if (event.isImplementationOf(jclass.events.IEvent)) {
                    if (typeof registeredEvents[event.type] !== 'undefined') {
                        var events = registeredEvents[event.type];
                        for (var i = 0; i < events.length; i++) {
                            events[i](event);
                        }
                    }
                } else {
                    throw new TypeError("Cannot dispatch objects that are not implementing `jclass.events.IEvent` interface");
                }
            },

            HasEventListener : function(eventType, callback) {
                if (typeof registeredEvents[eventType] !== 'undefined') {
                    return (registeredEvents[eventType].indexOf(callback) != -1);
                }
                return false;
            }
        },
        private : {
            registeredEvents : {}
        }
    }),


    Event : jClass.implement(jclass.events.IEvent)({
        constructor : function(eventType, dataObject, canStopPropagation) {
            this.type = eventType;
            this.data = dataObject;
            _canStopEvent = canStopPropagation;
        },
        public : {
            data : "",
            type : "EVENT",

            stopEventPropagation : function() {
                
            },

            clone : function() {

            }
        },
        private : {
            _canStopEvent : true
        },
        static : {
            DATA : "data",
            COMPLETE : "complete",
            ERROR : "error"
        }
    })
});